/*
 * Copyright © 2012 Avego Ltd., All Rights Reserved.
 * For licensing terms please contact Avego LTD.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.clkj.tramcarlibrary.service;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;

import com.clkj.tramcarlibrary.R;

public class GpsworkSetting {

	//private final static FFLog LOG = FFLog.getLogInstance(GpsworkSetting.class);

	/**
	 * This check gps setting and show dialog
	 * @return
	 */
	public static boolean checkSetting(Activity act) {
		if (isGpsOpen(act) || isNetGpsOpen(act)) {
			return true;
		}
		openGps(true, null, act);
		return false;
	}

	/**
	 * This
	 * @param exit
	 * @param run
	 * @return
	 */
	private static AlertDialog dialog;
	private static void openGps(final boolean exit, final Runnable run, final Activity act) {
		String message = "";
		String negativeBtnTitle = "";
		if (exit) {
			message = act.getString(R.string.gps_close);
			negativeBtnTitle = act.getString(R.string.exit);
		} else {
			message = act.getString(R.string.open_gps);
			negativeBtnTitle = act.getString(R.string.cancel);
		}
		GpsworkSetting.dialog = new AlertDialog.Builder(act).setTitle(act.getString(R.string.whoops)).setMessage(message)
				.setPositiveButton(act.getString(R.string.setting), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (!act.isFinishing()) {
							dialog.dismiss();
						}
						try {
							Intent fireAlarm = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
							fireAlarm.addCategory(Intent.CATEGORY_DEFAULT);
							act.startActivityForResult(fireAlarm, 0);
						} catch (Exception e) {
							act.startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
						}

					}
				}).setNegativeButton(negativeBtnTitle, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (!act.isFinishing()) {
							dialog.dismiss();
						}
						if (exit) {
							System.exit(0);
						} else {
							if (run != null) {
								new Thread(run).start();
							}
						}

					}
				}).create();
		GpsworkSetting.dialog.setCancelable(false);
		GpsworkSetting.dialog.show();
	}

	/**
	 * This check GPS whether is open
	 * @param inContext
	 * @return boolean
	 */
	public static boolean isGpsOpen(Activity inContext) {

		String serviceName = Context.LOCATION_SERVICE;
		LocationManager locationManager = (LocationManager) inContext.getSystemService(serviceName);
		if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			//LOG.debug("gps is enable");
			return true;
		}
		//LOG.debug("gps is not enable");
		return false;

	}

	/**
	 * this check whether can get location by network
	 * @param inContext
	 * @return boolean
	 */
	public static boolean isNetGpsOpen(Activity inContext) {
		String serviceName = Context.LOCATION_SERVICE;
		LocationManager locationManager = (LocationManager) inContext.getSystemService(serviceName);
		if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			//LOG.debug("netgps is enable");
			return true;
		}
		//LOG.debug("netgps is not enable");
		return false;
	}
	
	public void checkGpsOver() {
		if (GpsworkSetting.dialog != null && GpsworkSetting.dialog.isShowing()) {
			GpsworkSetting.dialog.dismiss();
		}
	}

}
